<?php
// +----------------------------------------------------------------------
// | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2018 http://www.thinkcmf.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 老猫 <thinkcmf@126.com>
// +----------------------------------------------------------------------
namespace app\common\model;  

use think\Model;

class UserLevelModel extends Model
{

   protected $name = 'user_level';


   //必须倒序查询
   function getAll(){
   	cache('all_level',null);
   	if(false ==($rows=cache('all_level'))){
   		$rows = $this->order('level desc')->select()->toArray();
   		if($rows) cache('all_level',$rows);
   	}
   	return $rows;
   }



   //计算用户级别
   function calLevel($invite_num,$group_num){
   		$levels = $this->getAll();
   		if(empty($levels)) return 0;

   		for($i=0;$i<count($levels);$i++){
   			if($invite_num>=$levels[$i]['invite_num'] && $group_num>=$levels[$i]['group_num']){
   				return $levels[$i]['level'];
   			}
   		}
   		return 0;
   }

}